<?php

class SupplierConsignmentListsController extends AppController {

    var $name = 'SupplierConsignmentLists';
    var $pageTitle = 'Consignment List';
    var $viewPath = 'suppliers/consignment_list';
    var $uses = array('Product', 'CustProductSite', 'Category', 'VSCustomer', 'Customer', 'Site', 'Supplier', 'Brand');

    function beforeFilter() {
        parent::beforeFilter();
        $this->set('parentNav', 'supplier_consignment_lists');
    }

    function display() {
        parent::display('Product');
        $op = array(
            'supplier_part_no' => 'LIKE',
            'category_id' => '=',
            'product_id' => '=',
            'brand_id' => '=',
            'model' => 'LIKE',
            'title' => 'LIKE',
            'text' => 'LIKE',
        );

        $contain = array('Supplier', 'Category', 'Brand');
        if (!empty($this->data['CustProductSite']['customer_id'])) {
            $product_ids = $this->CustProductSite->listProductIdByCustomerId($this->data['CustProductSite']['customer_id']);
            $this->data['Product']['id'] = $product_ids;
            $this->Session->write("CustProductSite.paging.customer_id", $this->data['CustProductSite']['customer_id']);
            $this->Customer->id = $this->data['CustProductSite']['customer_id'];
            $this->Session->write('Criteria.customer', $this->Customer->field('name'));
        }
        if (!empty($this->data['CustProductSite']['site_id'])) {
            $product_ids = $this->CustProductSite->listProductIdBySiteId($this->data['CustProductSite']['site_id'], 'supplier');
            $this->data['Product']['id'] = $product_ids;
            $this->Session->write("CustProductSite.paging.site_id", $this->data['CustProductSite']['site_id']);
            $this->Site->id = $this->data['CustProductSite']['site_id'];
            $this->Session->write('Criteria.site', $this->Site->field('name'));
            $contain['CustProductSite'] = array('conditions' => array('site_id' => $this->data['CustProductSite']['site_id']));
        }
        unset($this->data['CustProductSite']);

        if (!empty($this->data['Product']['category_id'])) {
            $this->Category->id = $this->data['Product']['category_id'];
            $this->Session->write('Criteria.category', $this->Category->field('name'));
        }

        if (!empty($this->data['Product']['brand_id'])) {
            $this->Brand->id = $this->data['Product']['brand_id'];
            $this->Session->write('Criteria.brand', $this->Brand->field('name'));
        }

        if (!empty($this->data['Product']['model'])) {
            $this->Session->write('Criteria.brand', $this->data['Product']['model']);
        }

        if (!empty($this->data['Product']['title'])) {
            $this->Session->write('Criteria.title', $this->data['Product']['title']);
        }

        if (!empty($this->data['Product']['supplier_part_no'])) {
            $this->data['Product']['supplier_part_no'] = trim($this->data['Product']['supplier_part_no']);
            $this->Session->write('Criteria.supplier_part_no', $this->data['Product']['supplier_part_no']);
        } elseif (!empty($this->data['Product']['part_no'])) {
            $this->data['Product']['supplier_part_no'] = trim($this->data['Product']['part_no']);
            $this->Session->write('Criteria.supplier_part_no', $this->data['Product']['supplier_part_no']);
            unset($this->data['Product']['part_no']);
        }

        $this->_checkSupplier('Product');

        unset($this->data['current_stock_operand']);
        $this->paginate['Product']['contain'] = $contain;
        $this->_paging('Product', $op);
        $this->set('supplier_id', $this->Auth->user('supplier_id'));
    }

    function view($id = null) {
        $this->Product->contain(array('Category', 'Supplier', 'Brand', 'Unit'));
        $this->data = $this->Product->findById($id);
        $this->Product->id = $id;

        $this->_neighbors($id, $this->Product, array('fields' => array('id', 'part_no')));
    }

    function popup_search() {
        parent::popup_search();

        $supplier_id = $this->Auth->user('supplier_id');
        $this->set('customers', $this->VSCustomer->findListByCustomerOnHand($this->Auth->user('id'), $supplier_id));
        $this->set('categories', $this->Category->listCategoryBySupplierId($supplier_id));
        $this->set('brands', $this->Product->Brand->find('list', array('conditions' => array('supplier_id' => $supplier_id))));
        $this->set('types', $this->Product->ItsType->find('list', array('field', array('type.id', 'type.code'), 'order' => 'name')));
        $this->set('popupWidth', 800);
    }

    /**
     * popup นี้จะเหมือนหน้า display เอามาแสดงเป็น popup สามารถเลือก product แล้วส่ง part no กลับไปหน้าที่เรียกมาได้
     */
    function popup_view() {
        $this->layout = 'popup';
        // clear session การ search จากครั้งก่อน
        $this->Session->delete('Product');
        $this->Session->delete('Products');
        $this->Session->delete("CustProductSite.paging.site_id");
        $this->Session->delete("CustProductSite.paging.customer_id");
        $supplier_id = $this->Auth->user('supplier_id');

        // ถ้ามีการส่ง site_id เข้ามา เพิ่มในเงื่อนไขการ search เลย
        if (isset($this->passedArgs['site_id'])) {
            $site_id = $this->passedArgs['site_id'];
            $this->Site->id = $site_id;
            $customer_id = $this->Site->field('customer_id');
            $this->set('sites', $this->Site->find('list', array('order' => 'customer_id, name', 'conditions' => array('customer_id' => $customer_id))));
        } else {
            $customer_id = null;
            $site_id = null;
            $this->set('sites', null);
        }

        $this->set('popupTitle', 'Select a Product');
        $this->set('popupWidth', '$j(document).width() - 100');

        // id ที่จะถูกเลือกเป็น default
        $this->set('site_id', $site_id);
        $this->set('customer_id', $customer_id);

        $this->set('categories', $this->Category->listCategoryBySupplierId($supplier_id));
        $this->set('brands', $this->Product->Brand->find('list', array('conditions' => array('supplier_id' => $supplier_id))));
        $this->set('customers', $this->VSCustomer->findListByCustomerOnHand($this->Auth->user('id'), $supplier_id));
    }

    /**
     * ใช้ร่วมกับ popup_view เป็นส่วนตารางแสดงแถว customers
     */
    function popup_paginate() {
        $this->Product->bindModel(array('hasMany' => array('CustProductSite')));
        if ($this->Session->check("{$this->name}.site_id")) {
            $this->data['CustProductSite']['site_id'] = $this->Session->read("{$this->name}.site_id");
        }
        
        if ($this->Session->check("{$this->name}.customer_id")) {
            $this->data['CustProductSite']['customer_id'] = $this->Session->read("{$this->name}.customer_id");
        }

        if (isset($this->passedArgs['site_id'])) {
            $this->data['CustProductSite']['site_id'] = $this->passedArgs['site_id'];
            $this->Session->write("{$this->name}.site_id", $this->data['CustProductSite']['site_id']);
        }
        if (isset($this->passedArgs['customer_id'])) {
            $this->data['CustProductSite']['customer_id'] = $this->passedArgs['customer_id'];
            $this->Session->write("{$this->name}.customer_id", $this->data['CustProductSite']['customer_id']);
        }

        $this->display();
    }

    function print_display($supplier_id) {
        $this->layout = 'print';
        $this->_setReportNote();
        $this->paginate['Product']['limit'] = 9999;
        $this->display();
        $this->render('print_display');
    }

    function excel_display($site_id) {
        parent::excel('VMI VENDOR INVENTORY');

        $this->_setReportNote();
        $this->paginate['Product']['limit'] = 9999;
        $this->display();
        $this->render('excel_display');
    }

    function _setReportNote() {
        $this->Supplier->id = $this->Auth->user('supplier_id');
        $this->set('supplier_no', $this->Supplier->field('code'));
        $this->set('supplier_name', $this->Supplier->field('name'));
        $this->set('report_by', $this->Auth->user('name'));
        $this->set('print_orientation', PRINT_LANDSCAPE);
    }

}

?>